﻿using MyWPF.StaticData;
using MyWPF.Views.Others;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media;

namespace MyWPF.ViewModel.Subpage.JournalPage_Son
{
    public class VM_JournalSet : BaseViewModel
    {
        public VM_JournalSet() 
        {
            #region 事件委托
            _close = new DelegateCommand<object>(_Close);
            _min = new DelegateCommand<object>(_Min);
            _setOnePageNum = new DelegateCommand<object>(_SetOnePageNum);
            #endregion
        }
        #region 基础：前端事件
        /// <summary>
        /// 关闭窗口
        /// </summary>
        private ICommand _close;
        public ICommand Close
        {
            get { return _close; }
            set
            {
                _close = value;
                OnPropertyChanged("Close");
            }
        }
        private void _Close(object obj)
        {
            StaticModels.journalSet.Close();
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        private ICommand _min;
        public ICommand Min
        {
            get { return _min; }
            set
            {
                _min = value;
                OnPropertyChanged("Min");
            }
        }
        private void _Min(object obj)
        {
            StaticModels.journalSet.WindowState = System.Windows.WindowState.Minimized;
        }


        private void OnPropertyChanged(string v)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region 属性绑定：当前页和总页数
        /// <summary>
        /// 一页日志数量
        /// </summary>
        public int OnePageNum
        {
            get { return _onePageNum; }
            set { SetProperty(ref _onePageNum, value); }
        }
        private int _onePageNum;
        #endregion
        #region 事件绑定：页面控制按钮
        /// <summary>
        /// 设置一页日志数量
        /// </summary>
        public ICommand SetOnePageNum
        {
            get { return _setOnePageNum; }
            set
            {
                _setOnePageNum = value;
                OnPropertyChanged("SetOnePageNum");
            }
        }
        private ICommand _setOnePageNum;
        private void _SetOnePageNum(object obj)
        {
            StaticModels.staticInfo.config.JournalConfig.OnePageNum = OnePageNum;
            //将StaticModels.staticInfo.config序列化后保存
            string configStr = JsonConvert.SerializeObject(StaticModels.staticInfo.config);
            var filePath = "Assets/Json/Config.json";
            File.WriteAllText(filePath, configStr);
            //刷新界面
            StaticModels.vM_JournalPage.RefreshJournal();
            Tips tips = new Tips("设置成功！");
            tips.ShowDialog();
        }
        #endregion

        
    }
}
